﻿using Corina.Datamodel.Entities;
using Corina.Enumerations;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Corina.DataContext.Service
{
    public class UserKindService : ServiceBase
    {
        public List<UserKindInfo> GetAllItems(string connectionString, bool loadAll = false)
        {
            StringBuilder builder = new StringBuilder();
            List<SqlParameter> paras = new List<SqlParameter>();

            builder.Append("SELECT * FROM t_user_kind WHERE 1=1 ");
            if(!loadAll)
            {
                builder.Append("AND IsAvailable = @Availability");
                paras.Add(new SqlParameter("@Availability", true));
            }

            return DbOperator.GetEntities<UserKindInfo>(connectionString, builder.ToString(), paras.ToArray());
        }

        public bool Update(string connectionString, UserKindInfo entity, EntityOperation operation)
        {
            return DbOperator.UpdateEntity(connectionString, entity, operation);
        }

        public bool ChangeAvailability(string connectionString, int id, bool availability)
        {
            string queryString = "UPDATE t_user_kind SET IsAvailable = @Availability WHERE Id=@Id";
            List<SqlParameter> paras = new List<SqlParameter>();
            paras.Add(new SqlParameter("@Id", id));
            paras.Add(new SqlParameter("@Availability", availability));
            return DbOperator.UpdateEntity(connectionString, queryString, paras.ToArray());
        }
    }
}
